﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary> State
/// by lzh 2017.08.10
/// </summary>

namespace QFramework
{
    public static class RelationshipExtension
    {
        public static bool ContainsKey(this relationshipList m_Ship, string key)
        {
            return m_Ship.map.Find(x => x.key.Equals(key)) != null;
        }

        public static void Add(this relationshipList m_Ship, string Key,string State)
        {
            if (m_Ship.ContainsKey(Key))
            {
                return;
            }

            m_Ship.map.Add(new relationship (Key,State));
        }

        public static void Remove(this relationshipList m_Ship, string key)
        {
            if (m_Ship.ContainsKey(key))
            {
                m_Ship.map.Remove(m_Ship.map.Find(x => x.key.Equals(key)));
            }
        }
    }

    [System.Serializable]
    public class relationship
    {
        [SerializeField]
        public string                                               key;
        [SerializeField]
        public string                                               State;

        public relationship(string key,string State)
        {
            this.key = key;
            this.State = State;
        }
    }

    [System.Serializable]
    public class relationshipList
    {        
        [SerializeField]
        public List<relationship>                                   map = new List<relationship>();
        public relationshipList()
        {
            map = new List<relationship>();
        }

        public string this[string index]
        {
            get { return map.Find(x => x.key.Equals(index)).State; }
        }
    }

    [System.Serializable]
    public class State
    {
        [SerializeField]
        protected StateControl                                      self;
        [SerializeField]
        //private Dictionary<string, string> map = new Dictionary<string, string>();
        protected relationshipList                                  map =new relationshipList ();
        [SerializeField]
        private List<IAction>                                       m_Action = new List<IAction>();
        private SequenceNodeChain                                   m_Chain;
        [SerializeField]
        protected string                                            stateID;
        public string                                               ID { get { return stateID; } }
        [SerializeField]
        private string                                              Finish;

        [SerializeField]
        private Rect                                                m_Pos;

        public State() { }
        public State(StateControl AI)
        {
            self = AI;
        }

        /// <summary>
        /// 为状态加入执行条件
        /// </summary>
        /// <param name="transition"></param>
        /// <param name="id"></param>
        public void AddTransition(string key, string state)
        {
            if (key == null || state == null)
            {
                return;
            }

            if (map.ContainsKey(key))
            {
                return;
            }

            map.Add(key, state);
        }
        /// <summary>
        /// 删除执行状态的条件
        /// </summary>
        /// <param name="myTransition"></param>
        public void DeleteTransition(string key)
        {
            if (key == null)
            {
                return;
            }

            if (map.ContainsKey(key))
            {
                map.Remove(key);
                return;
            }
        }

        /// <summary>
        /// 根据条件获得状态
        /// </summary>
        /// <param name="trans"></param>
        /// <returns></returns>
        public string GetOutputState(string trans)
        {
            if (trans == null)
            {
                return null;
            }
            if (map.ContainsKey(trans))
            {
                return map[trans];
            }
            return null;
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="player"></param>
        /// <param name="npc"></param>
        public virtual void update()
        {

        }

        /// <summary>
        /// 进入状态
        /// </summary>
        public virtual void EnterState()
        {
            m_Chain = new SequenceNodeChain() { Executer = self };
            foreach (var action in m_Action)
            {
                m_Chain.Append(action);
            }
            m_Chain
                .Begin()
                .DisposeWhenFinished()
                .OnDisposed(() => { self.Event(Finish); });
        }

        /// <summary>
        /// 离开状态
        /// </summary>
        public virtual void ExitState()
        {
            if (m_Chain != null)
            {
                m_Chain.Break();
            }
        }

    }
}
